package com.weitu.accidentEvent.accident.controller;

import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.accidentEvent.accident.entity.Accident;
import com.weitu.accidentEvent.accident.service.AccidentReportService;
import com.weitu.accidentEvent.accident.service.AccidentService;
import com.weitu.base.controller.BaseController;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * 事故后评估controller
 * Created by fanyuting on 2017/5/19 0019.
 */
@Controller
@RequestMapping("accidentEvent/accidentEvaluation")
public class AccidentEvaluationController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH+"accidentEvent/accidentEvaluation/";
    @Autowired
    private AccessoryUtilService accessoryUtilService;//附件
    @Autowired
    private AccidentService accidentService;//事故信息service
    @Autowired
    private AccidentReportService accidentReportService;//事故报告service
    /**
     * 跳转到事故后评估列表页面
     * @param model
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String toAccidentEvaluationListPage(Model model){
        return MODULE_BATH_PATH+"list";
    }
    /**
     * 跳转到新增页面
     * @param model
     * @return
     */
    @RequestMapping(value = "evaluation",method = RequestMethod.GET)
    public String toAccidentEvaluationAddPage(HttpServletRequest request,Model model){
        String accidentReportId = request.getParameter("accidentReportId");
        Map<String,Object> accidentReport = this.accidentReportService.queryInfoById(accidentReportId);
        String accidentId = accidentReport.get("accidentId").toString();
        Accident accident = this.accidentService.queryInfoById(accidentId);

        model.addAttribute("accident",accident);
        model.addAttribute("accidentReport",accidentReport);
        
        return MODULE_BATH_PATH+"evaluation";
    }
    @ResponseBody
    @RequestMapping("/editAccidentEvaluationInfo")
    @Transactional
    public AjaxResult editAccidentReportInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        putParametersIntoMap(map);
        boolean accFlag =false;
        boolean flag = this.accidentReportService.editEducationSituation(map);
        if(flag){
            accFlag = AccessoryUtil.saveAccerssoryInfo(map.get("id").toString());
        }
        if(accFlag){
            return success("事故后评估信息保存成功！");
        }
        return fail("事故后评估信息保存失败！");
    }
}
